package com.example.lianguisystem.config;

import com.example.lianguisystem.DTO.UserList;
import com.example.lianguisystem.mapper.UtilMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class PostConstructUtil {

    @Autowired
    private UtilMapper utilMapper;

    public static List<UserList> userLists;

    public static Map<String, String> getUserMap() {
        Map<String, String> userMap = new HashMap<>();
        List<UserList> userLists = PostConstructUtil.userLists;
        for (UserList user : userLists) {
            String userId = user.getUserid();
            if (userMap.containsKey(user.getName())) {
                userId = userId + "|" + userMap.get(user.getName());
                userMap.put(user.getName(), userId);
            }
            userMap.put(user.getName(), userId);
        }
        return userMap;
    }
    @Bean
    InitializingBean initializingBean() {
        return () -> {
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            long start = System.currentTimeMillis();
            newGetAllUser();
            long end = System.currentTimeMillis();
            System.out.println(timestamp + ":" + (end - start)/1000 +"秒");
            log.warn("PostConstructUtil启动启动启动启动启动启动启动启动启动");
        };
    }

    private void newGetAllUser() {
        userLists = utilMapper.selectQYWXAllUser();
    }
}
